C#: Using DataGridView

Categories: Development Notes; Tagged with: ; @ January 16th, 2013 17:34

Requirement:

Display data in datagrid.

image

Solution:

Use C# DataGridView, and put all objects in one list, assign the list as the datagrid’s DataSource.

Details

 

0. Prepare the Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSISHelper.com.liguoliang.ssis.util
{

    class LogResult
    {
        public LogResult(String result, TimeSpan timeSpan, DateTime dateTime, String logName)
        {
            this.result = result;
            this.timeSpan = timeSpan;
            this.dateTime = dateTime;
            this.logName = logName;
        }
            
        public String result { get; set; }
        public TimeSpan timeSpan { get; set; }
        public DateTime dateTime { get; set; }
        public String logName { get; set; }
    }
}

1.  Drag one DataGridView, create columns:

            this.tabPage2.Controls.Add(this.dataGridViewForLogs);
this.dataGridViewForLogs.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.result,
            this.TimeSpan,
            this.DateTime,
            this.LogFile});

this.dataGridViewForLogs.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.result,
            this.TimeSpan,
            this.DateTime,
            this.LogFile});

2. Set the datasource:

        private void btnAnalysisLogs_Click(object sender, EventArgs e)
        {
            dataGridViewForLogs.DataSource = LogUtils.analysisLogs(textBoxPath.Text);
        }

// Generate the list
 public static ArrayList analysisLogs(String rootPath)
 {
            ArrayList listLogResults = new ArrayList();

            
            foreach (String pathLogFile in logFiles)
            {
              LogResult logResult = new LogResult(strLogResult, span, File.GetLastWriteTime(pathLogFile), diLog.Name);
                listLogResults.Add(logResult);
            }

            return listLogResults;
        }

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.